﻿using log4net;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CloudDcPc
{
    /// <summary>
    /// Login.xaml 的交互逻辑
    /// </summary>
    public partial class Login : Window
    {
        private string user_num;
        private string pwd;
        public Login()
        {
            InitializeComponent();

            if (Properties.Settings.Default.set_save_pwd)
            {
                txt_user_num.Text = Properties.Settings.Default.save_user_num;
                txt_pwd.Password = Properties.Settings.Default.save_user_pwd;
                ccb_savapwd.IsChecked = true;
            }
            if (Properties.Settings.Default.set_auto_login)
            {
                ccb_autologin.IsChecked = true;
                btn_login.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            user_num = txt_user_num.Text;
            pwd = txt_pwd.Password;

            btn_login.IsEnabled = false;
            btn_login.Content = "登录中...";
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += bw_DoWork;    //调用RunWorkerAsync()时发生 所以放在前面
            bw.RunWorkerAsync();   
            
            bw.RunWorkerCompleted += bw_RunWorkerCompleted;


        }

        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            int res = (int)e.Result;  
            if (res == 1)
            {
                if (ccb_savapwd.IsChecked == true)
                {
                    Properties.Settings.Default.save_user_num = txt_user_num.Text;
                    Properties.Settings.Default.save_user_pwd = txt_pwd.Password;
                }
                Properties.Settings.Default.set_auto_login = (bool)ccb_autologin.IsChecked;
                Properties.Settings.Default.set_save_pwd = (bool)ccb_savapwd.IsChecked;
                Properties.Settings.Default.Save();

                Main main = new Main(txt_user_num.Text);
                main.Show();
                this.Close();
            }
            else if (res == 0)
            {
                btn_login.IsEnabled = true;
                btn_login.Content = "登录";
                txtb_errorinfo.Visibility = Visibility.Visible;
                txtb_errorinfo.Text = "员工号或密码错误";
            }
            else if (res == -1)
            {
                btn_login.IsEnabled = true;
                btn_login.Content = "登录";
                txtb_errorinfo.Visibility = Visibility.Visible;
                txtb_errorinfo.Text = "数据库连接失败";
            }
            else if (res == -2)
            {
                btn_login.IsEnabled = true;
                btn_login.Content = "登录";
                txtb_errorinfo.Visibility = Visibility.Visible;
                txtb_errorinfo.Text = "身份错误";
            }
        }

        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = App.UserBean.VerifyUser(user_num, pwd);
        }

        private void CheckBox_Checked_1(object sender, RoutedEventArgs e)
        {
            ccb_savapwd.IsChecked = true;
        }

        private void ccb_savapwd_Unchecked(object sender, RoutedEventArgs e)
        {
            ccb_autologin.IsChecked = false;
        }
    }
}
